package com.zshan.clinic.web.controller.medical;


import com.zshan.clinic.admin.entity.dto.OrderDispenseDTO;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.service.medical.OrderDispenseService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.OrderDelivery;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 药房
 * @author lichen
 */
@RestController
@RequestMapping("/order/dispense")
public class OrderDispenseController extends AbstractController {


    @Autowired
    private OrderDispenseService orderDispenseService;

    /**
     * 患者：查询患者发药记录
     * @param patientId 患者ID
     * @param visitDate 就诊日期
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPatientDeliveryList")
    public RestResp<PageInfo<OrderDeliveryPatientVo>> getPatientDeliveryList(@RequestParam(name = "patientId") Long patientId,
                                                                             @RequestParam(name = "visitDate",required = false) LocalDate visitDate,
                                                                             @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                             @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        PageInfo<OrderDeliveryPatientVo> deliveryList = orderDispenseService.getPatientDeliveryList(userInfo.getClinicId(), patientId,visitDate,pageNo,pageSize);
        return RestResp.SUCCESS(deliveryList);
    }


    /**
     * 药房配药：查询今日药品待发数
     * @return
     */
    @GetMapping("/getWaitingCount")
    public RestResp<DispenseWaitingVo> getWaitingCount() {
        UserInfo userInfo = getUserInfo();
        return RestResp.SUCCESS(orderDispenseService.getWaitingCount(userInfo.getClinicId()));
    }

    /**
     * 药房配药：获取配药单列表
     * @param patientName 患者名
     * @param visitDate 就诊日期，如果不给值默认当天
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getDispenseList")
    public RestResp<List<OrderDispensePageVo>> getDispenseList(@RequestParam(name = "patientName", required = false) String patientName,
                                                               @RequestParam(name = "visitDate", required = false) LocalDate visitDate,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderDispensePageVo> list = orderDispenseService.getDispenseList(userInfo.getClinicId(), patientName, visitDate, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }

    /**
     * 药房配药：查询配药单详情
     * @param orderId 订单ID，从列表中获取
     * @return
     */
    @GetMapping("/getDispenseDetail")
    public RestResp<OrderDispenseDetailVo> getDispenseDetail(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        OrderDispenseDetailVo detail = orderDispenseService.getDispenseDetail(userInfo.getClinicId(),orderId);
        return RestResp.SUCCESS(detail);
    }

    /**
     * 药房配药：获取患者配药历史
     * @param patientId 患者ID从列表中获取
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getDispenseHistoryList")
    public RestResp<List<OrderDeliveryDetailVo>> getDispenseHistoryList(@RequestParam(name = "patientId") Long patientId,
                                                                        @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderDeliveryDetailVo> list = orderDispenseService.getDispenseHistoryList(userInfo.getClinicId(), patientId, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }


    /**
     * 药房配药：完成配药
     * @param orderDispenseDTO
     * @return
     */
    @PostMapping("/completeDispense")
    public RestResp<Void> completeDispense(@RequestBody OrderDispenseDTO orderDispenseDTO) {
        UserInfo userInfo = getUserInfo();
        orderDispenseDTO.setUpdateUserId(userInfo.getEmpId());
        orderDispenseDTO.setUpdateBy(userInfo.getEmpName());
        orderDispenseDTO.setClinicId(userInfo.getClinicId());
        orderDispenseService.completeDispense(orderDispenseDTO);
        return RestResp.SUCCESS();
    }


    /**
     * 药房发药：获取发药单列表
     * @param patientName 患者名
     * @param visitDate 就诊日期 yyyy-MM-dd
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getDeliveryList")
    public RestResp<List<OrderDeliveryPageVo>> getDeliveryList(@RequestParam(name = "patientName", required = false) String patientName,
                                                               @RequestParam(name = "visitDate", required = false) LocalDate visitDate,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderDeliveryPageVo> list = orderDispenseService.getDeliveryList(userInfo.getClinicId(), patientName, visitDate, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }


    /**
     * 药房发药：查询发药单详情
     * @param orderId 订单ID，从列表中获取
     * @return
     */
    @GetMapping("/getDeliveryDetail")
    public RestResp<OrderDeliveryDetailVo> getDeliveryDetail(@RequestParam(name = "orderId") Long orderId) {
        UserInfo userInfo = getUserInfo();
        OrderDeliveryDetailVo detail = orderDispenseService.getDeliveryDetail(userInfo.getClinicId(),orderId);
        return RestResp.SUCCESS(detail);
    }

    /**
     * 药房发药:获取患者发药历史
     * @param patientId 患者ID
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getDeliveryHistoryList")
    public RestResp<List<OrderDeliveryDetailVo>> getDeliveryHistoryList(@RequestParam(name = "patientId") Long patientId,
                                                                        @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<OrderDeliveryDetailVo> list = orderDispenseService.getDeliveryHistoryList(userInfo.getClinicId(), patientId, pageNo, pageSize);
        return RestResp.SUCCESS(list);
    }


    /**
     * 发药单审核
     * @param orderDelivery
     * @return
     */
    @PostMapping("/rejectDelivery")
    public RestResp<Void> rejectDelivery(@RequestBody OrderDelivery orderDelivery) {
        UserInfo userInfo = getUserInfo();
        orderDelivery.setUpdateUserId(userInfo.getEmpId());
        orderDelivery.setUpdateBy(userInfo.getEmpName());
        orderDelivery.setClinicId(userInfo.getClinicId());
        orderDispenseService.reviewDelivery(orderDelivery);
        return RestResp.SUCCESS();
    }
}
